import sys
import requests
from bs4 import BeautifulSoup

def get_url():
    if len(sys.argv) > 1:
        return sys.argv[1]
    else:
        return input("请输入网址：")

def get_text_from_url(url):
    try:
        response = requests.get(url)
        response.raise_for_status()  # 如果请求返回的状态码不是200，则抛出异常
        soup = BeautifulSoup(response.text, 'html.parser')
        element = soup.select_one('.widget-episodeBody.js-episode-body')
        if element:
            return element.get_text(separator='\n',strip=True)
        else:
            return "未找到指定元素"
    except requests.RequestException as e:
        return f"请求错误：{e}"
    except Exception as e:
        return f"发生错误：{e}"

def main():
    url = get_url()
    text = get_text_from_url(url)
    with open('output.txt', 'w', encoding='utf-8') as f:
        f.write(text)
    print("内容已导出到output.txt")

if __name__ == "__main__":
    main()